package com.longrise.android.byjk.plugins.vip.integralexchange;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;

/**
 * Created by callxkj on 2017/8/2.
 * 积分任务的工具类
 */

public class IntegralTaskUtil {

    public static void toIntegralTask(final Activity activity) {
        EntityBean bean = new EntityBean();
        bean.set("devicetype", "android");
        bean.set("urltype", "jfrw");
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        DZZWTools.showWaitDialog(activity);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_getAppEntryPath", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            if (result != null) {
                                EntityBean resultBean = (EntityBean) result;
                                parseResult(activity, resultBean);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        DZZWTools.showToast(activity,AppUtil.getString(R.string.nonetwork),0);
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        DZZWTools.dismissDialog(null);
                    }
                });
    }

    /**
     * 解析积分任务结果并跳转
     */
    private static void parseResult(Activity activity, EntityBean bean) {
        EntityBean resultBean = bean.getBean("result");
        int resultstate = (Integer) bean.get("resultstate");
        String resultdesc = bean.getString("resultdesc");
        if (resultstate == 1) {
            String pageurl = resultBean.getString("pageurl");
            Bundle bundle = new Bundle();
            int screenWidth = AppUtil.getScreenWidth();
            int widthDP = AppUtil.px2dip(screenWidth);
            String userid = UserInfor.getInstance().getUserid();
            String usersfzh = UserInfor.getInstance().getUsersfzh();
            pageurl = pageurl + "?cardno=" + usersfzh + "&userid=" + userid + "&screenwidth=" + widthDP;
            String mrrwurl = pageurl + "&type=1"; //每日任务
            String jbrwurl = pageurl + "&type=2";  //基本任务

//            bundle.putString(IntegralTaskActivity.BASIC_URL, jbrwurl);
//            bundle.putString(IntegralTaskActivity.DAILY_URL, mrrwurl);
//            Intent intent = new Intent(activity, IntegralTaskActivity.class);
//            intent.putExtras(bundle);
//            activity.startActivity(intent);
        } else {
            DZZWTools.showToast(activity, resultdesc, 0);
        }
    }


}
